/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.table.explorer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import twaver.Element;
import twaver.Generator;
import twaver.Node;
import twaver.TDataBox;
import twaver.VisibleFilter;
import twaver.table.TTreeTable;
import demo.DemoPane;

public class ExplorerDemo extends DemoPane{
	private TDataBox box = new TDataBox();
	private TTreeTable table = new ExplorerTable(box);
	private JLabel label = new JLabel("");
	
	public ExplorerDemo(){
		FileLoader loader = new FileLoader(box, label);
		table.getTree().setLazyLoader(loader);
		table.setTableBodyPopupMenuFactory(new PopupMenuFactory(box));
		table.getTree().setRootVisible(false);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		table.setElementClass(Node.class);
		table.registerElementClassXML(Node.class, "/demo/table/explorer/FileElement.xml");
		table.setRowHeight(18);
		table.setShowGrid(false);
		table.setEditable(false);
		table.setTreeColumnDisplayName("File");

		table.getTree().setElementLabelGenerator(new Generator(){
			public Object generate(Object object) {
				Element element = (Element)object;
				FileInfo info = (FileInfo)element.getBusinessObject();
				return info.getFileName();
			}
		});
		table.getTree().setElementToolTipTextGenerator(new Generator(){
			public Object generate(Object object) {
				Element element = (Element)object;
				FileInfo info = (FileInfo)element.getBusinessObject();
				return info.getFile().getAbsolutePath();
			}
		});
		
		label.setOpaque(true);
		label.setBackground(Color.BLACK);
		label.setForeground(Color.GREEN);
		
		final JCheckBox displayFileNode = new JCheckBox("Display File Node", true);
		displayFileNode.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				table.getTree().updateTViewUI();
			}
		});
		table.getTree().addVisibleFilter(new VisibleFilter(){
			public boolean isVisible(Element element) {
				FileInfo info = (FileInfo)element.getBusinessObject();
				if(displayFileNode.isSelected()){
					return true;
				}else{
					return info.getFile().isDirectory();
				}
			}
		});
		JPanel pane = new JPanel(new BorderLayout());
		pane.add(displayFileNode, BorderLayout.WEST);
		pane.add(label, BorderLayout.CENTER);
		this.add(new JScrollPane(table), BorderLayout.CENTER);
		this.add(pane, BorderLayout.SOUTH);	

	}

}